<template>
  <!-- 发布评论的弹出层 -->
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      @click="addCommentFn"
      :disabled="!message.length"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
import eventBus from "@/utlis/bus";
export default {
  name: "CommentPost",
  components: {},
  props: ["target"], //当评论的时候才有target
  data() {
    return {
      message: "",
    };
  },
  // inject: ["articleId"], //提供给后代数据的方法，此为文章id
  computed: {},
  watch: {},
  created() {
    console.log("组件创建");
  },
  destroyed() {
    console.log("组件销毁");
  },
  mounted() {},
  methods: {
    async addCommentFn() {
      this.$toast.loading({
        message: "发布中...",
        forbidClick: true,
      });
      try {
        let {
          data: {
            data: { new_obj },
          },
        } = await addComment({
          target: this.target || this.$route.params.articleId, //文章id
          content: this.message,
          art_id: this.target && this.$route.params.articleId,
        });
        this.message = "";
        //  父关闭弹层
        this.$emit("closepoup");
        // 更新数据列表list
        eventBus.$emit("updataComment", new_obj);
        this.$toast.success("发布成功");
      } catch (error) {
        console.log(error);
        this.$toast.fail("发布失败,请稍后重试");
      }
    },
  },
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
